﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestStarter.Model
{
    public class TestGroupDetailed
    {
        # region Private Variables

        private Nullable<int> _passedItems { get; set; }
        private Nullable<int> _failedItems { get; set; }
        private Nullable<int> _skipedItems { get; set; }
        private DateTime _testStartDate { get; set; }
        private DateTime _testEndDate { get; set; }

        # endregion

        public ObservableCollection<TestGroupItem> GroupItemList { get; set; }

        public string GroupName { get; set; }

        public Nullable<int> PassedItems
        {
            get
            {
                if (this._passedItems == null)
                {
                    this._passedItems = 0;
                    foreach (var gil in this.GroupItemList)
                    {
                        if(gil.Status.Equals(ItemStatus.PASSED))
                        {
                            this._passedItems += 1;
                        }
                    }
                }

                return this._passedItems;
            }
            set
            {
                this._passedItems = value;
            }
        }

        public Nullable<int> FailedItems
        {
            get
            {
                if (this._failedItems == null)
                {
                    this._failedItems = 0;
                    foreach (var gil in this.GroupItemList)
                    {
                        if (gil.Status.Equals(ItemStatus.FAILED))
                        {
                            this._failedItems += 1;
                        }
                    }
                }

                return this._failedItems;
            }
            set
            {
                this._failedItems = value;
            }
        }

        public Nullable<int> SkipedItems
        {
            get
            {
                if (this._skipedItems == null)
                {
                    this._skipedItems = 0;
                    foreach (var gil in this.GroupItemList)
                    {
                        if (gil.Status.Equals(ItemStatus.SKIPPED))
                        {
                            this._skipedItems += 1;
                        }
                    }
                }

                return this._skipedItems;
            }

            set
            {
                this._skipedItems = value;
            }
        }

        public DateTime GroupTestStartDate 
        {
            get
            {
                if (this._testStartDate == new DateTime())
                {
                    this._testStartDate = DateTime.Today;

                    foreach (var gil in GroupItemList)
                    {
                        if (DateTime.Compare(this._testStartDate, gil.ItemTestStartDate) > 0)
                            this._testStartDate = gil.ItemTestStartDate;

                    }
                }
                return this._testStartDate;
            }

            set
            {
                this._testStartDate = value;
            }
        }

        public DateTime GroupTestEndDate
        {
            get
            {
                if (this._testEndDate == new DateTime())
                {
                    this._testEndDate = DateTime.Today;

                    foreach (var gil in GroupItemList)
                    {
                        if (DateTime.Compare(this._testEndDate, gil.ItemTestStartDate) < 0)
                            this._testEndDate = gil.ItemTestStartDate;

                    }
                }
                return this._testEndDate;
            }

            set
            {
                this._testEndDate = value;
            }
        }

        public int Age
        {
            get
            {
                return (int)DateTime.Today.Subtract(this.GroupTestStartDate).TotalDays;
            }
        }

        public int Duration
        {
            get
            {
                return this.GroupTestEndDate.Subtract(this.GroupTestStartDate).Seconds;
            }
        }
    }
}
